USE Lib
GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
----------------------------
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'fn_recs_gen') BEGIN
	DROP  FUNCTION  tsql.fn_recs_gen
	PRINT '<<< DROP FUNCTION tsql.recs_gen - Completed with SUCCESS >>>'
END

GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*================================================================================================
Name:			tsql.sp_recs_gen
Type:			User defined function 
Owner:					
Description:	Generates requested number of records 
Parameters:		@recs_to_gen = number of records to generate
				@rand_flag = generates a random number column to join with other tables and 
							 generate addresses, names, phone numbers, etc.
Dependens on:  	fn_rnd
Usage:			SELECT * FROM tsql.fn_recs_gen(1000)
--------------------------------------------------------------------------------------------------
Project:		http://sqllib.codeplex.com/
Help:			http://sqllib.codeplex.com/wikipage?title=sp_recs_gen&referringTitle=Documentation		
Version:		20120827
SrcCtrlApp:		SVN
SrcCtrlPath:	http://sqllib.codeplex.com/SourceControl/list/changesets
Note:*			CTRL + click to follow the links above
--------------------------------------------------------------------------------------------------
History:		11/07/2008 - Adrian E Dudau - Function created
History:		12/16/2009 - Adrian E Dudau - Changed param @recs_to_gen to BIGINT 			
History:		12/16/2009 - Adrian E Dudau - Added random id feature, can be used to link to other tables 
											  and generate names, adresses, etc.  		
History:		08/22/2012 - Adrian E Dudau - Added references to project, SVN and Help											  	
History:		08/27/2012 - Adrian E Dudau - Used fn_rnd function the gen random id											  	
================================================================================================*/

CREATE FUNCTION [tsql].[fn_recs_gen] (@recs_to_gen BIGINT, @rand_flag BIT)
RETURNS @Result TABLE (id BIGINT IDENTITY(1,1), rand_id BIGINT)
AS

BEGIN
	  
	DECLARE @i BIGINT = 1   
	WHILE @i <= @recs_to_gen BEGIN
		IF @rand_flag = 1 BEGIN		 
			INSERT INTO @Result (rand_id) 
			VALUES(Lib.tsql.fn_rnd(NULL) * @recs_to_gen + @i);			
		END	
		ELSE BEGIN 
			INSERT INTO @Result (rand_id) VALUES (NULL);
		END
			
		SET @i = @i + 1	
	END /*WHILE*/

	-- NOTE: for values over 100,000 it may be worth 
	--		 to insert more that one row for performance improv
	
	RETURN 
END

GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
-- Verify completion
----------------------------
IF EXISTS(SELECT * FROM sysobjects WHERE name = 'fn_recs_gen') BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_recs_gen - Completed with SUCCESS >>>'
END

ELSE BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_recs_gen - Completed with ERROR >>>'
END


GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------

--  GRANT SELECT TO PUBLIC will be used at Db level
